package com.aier.classes.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.classes.entity.ClassStudent;
import com.aier.common.common.SystemDateFormatUtil;



public class ClassStudentRowMapper implements RowMapper<ClassStudent> {

    private int type;

    public static final int AND_NAME = 1;
    public static final int AND_CR = 2;
    public static final int AND_SEX = 3;
    
    public ClassStudentRowMapper() {
    }

    public ClassStudentRowMapper(int type) {
        this.type = type;
    }

    @Override
    public ClassStudent mapRow(ResultSet rs, int rowNum) throws SQLException {
    	ClassStudent cs = parseClassStudent(new ClassStudent(), rs);
    	cs.setCtime_str(SystemDateFormatUtil.formatDate(cs.getCtime(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));

    	switch (type) {
            case AND_NAME:
                cs.setName_classes(rs.getString("name_classes"));
                try {
                    cs.setCardnum(rs.getString("cardnum"));
                } catch (SQLException e) {
                    cs.setCardnum(null);
                }
                break;
            case AND_CR:
                cs.setLeav(rs.getInt("leav"));
                cs.setAbsent(rs.getInt("absent"));
                break;
            case AND_SEX:
                cs.setSex(rs.getString("sex"));
                break;
    	}
        return cs;
    }

    public static ClassStudent parseClassStudent(ClassStudent s, ResultSet rs) throws SQLException {
        return parseClassStudent(s, rs, null);
    }

    public static ClassStudent parseClassStudent(ClassStudent s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setCsid(rs.getLong(su+"csid"));
        } catch (SQLException e) {
            s.setCsid(null);
        }
        try {
            s.setUid(rs.getLong(su+"uid"));
        } catch (SQLException e) {
            s.setUid(null);
        }
        try {
            s.setSname(rs.getString(su+"sname"));
        } catch (SQLException e) {
            s.setSname(null);
        }
        try {
            s.setCid(rs.getLong(su+"cid"));
        } catch (SQLException e) {
            s.setCid(null);
        }
        try {
            s.setCtime(rs.getTimestamp(su+"ctime"));
        } catch (SQLException e) {
            s.setCtime(null);
        }
        
       
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, ClassStudent s) {
    	par.put("csid",s.getCsid());
    	par.put("uid",s.getUid());
        par.put("sname", s.getSname());
        par.put("cid",s.getCid());
        par.put("ctime",s.getCtime());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, ClassStudent s) {
        args.put("csid",s.getCsid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final ClassStudent s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "csid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(ClassStudent s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into class_student(");
        int count = 0;
        if (null != s.getUid()) {
            sql.append("uid,");
            count++;
        }

        if (null !=s.getSname()) {
            sql.append("sname,");
            count++;
        }
        if (null != s.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null != s.getCtime()) {
            sql.append("ctime,");
            count++;
        }

        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, ClassStudent s)
        throws SQLException {
        int count = 1;
        if (null != s.getUid()) {
            ps.setLong(count++,s.getUid());
        }
        if (null != s.getSname()) {
            ps.setString(count++, s.getSname());
        }
        if (null != s.getCid()) {
            ps.setLong(count++,s.getCid());
        }
        if (null != s.getCtime()) {
            ps.setTimestamp(count++,s.getCtime());
        }
        
       


    }

}
